package com.example.tse.rbhttpservice.service.moments;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsCommentGetList;
import com.example.tse.rbhttpservice.service.moments.model.MomentsCommentGetListRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsCommentGetListResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsCommentRemoveRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsCommentRemoveResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsCommentSubmitRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsCommentSubmitResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class MomentsCommentServiceImpl implements MomentsCommentService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public MomentsCommentServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 删除朋友圈中帖子的评论，用户能删除个人发表的评论，长按弹出删除
	 * @param request
	 * @param listener
	 * @return MomentsCommentRemoveResponse
	 */		
	 
	    public void remove(MomentsCommentRemoveRequest request, final RemoteServiceListenerT<MomentsCommentRemoveResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("commentId",request.getCommentId());

        final String url = "moments/comment/remove";


            new HttpService().post(context, url, params, MomentsCommentRemoveResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsCommentRemoveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsCommentRemoveResponse() : (MomentsCommentRemoveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsCommentRemoveResponse() : (MomentsCommentRemoveResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 评论内容140字
	 * @param request
	 * @param listener
	 * @return MomentsCommentSubmitResponse
	 */		
	 
	    public void submit(MomentsCommentSubmitRequest request, final RemoteServiceListenerT<MomentsCommentSubmitResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("commentId",request.getCommentId());
            params.put("memberId",request.getMemberId());
            params.put("content",request.getContent());

        final String url = "moments/comment/submit";


            new HttpService().post(context, url, params, MomentsCommentSubmitResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsCommentSubmitResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsCommentSubmitResponse() : (MomentsCommentSubmitResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsCommentSubmitResponse() : (MomentsCommentSubmitResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 获取朋友圈某帖子评论回复列表，回复评论只能层级一次（如xxx回复yyy：123abc）
	 * @param request
	 * @param listener
	 * @return MomentsCommentGetListResponse
	 */		
	 
	    public void getList(MomentsCommentGetListRequest request, final RemoteServiceListenerT<MomentsCommentGetListResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("momentId",request.getMomentId());

        final String url = "moments/comment/getList";


        
        /**
        comment
        getList
        GetList
        获取朋友圈某帖子评论回复列表，回复评论只能层级一次（如xxx回复yyy：123abc）
        GET
        MomentsComment
        MomentsCommentGetList
        moments/comment/getList
        array
        */
	
           final Type type = new TypeToken<List<MomentsCommentGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MomentsCommentGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MomentsCommentGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MomentsCommentGetListResponse responce = new MomentsCommentGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MomentsCommentGetListResponse());
                 } else {
                     List<MomentsCommentGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsCommentGetListResponse responce = new MomentsCommentGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MomentsCommentGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MomentsCommentGetListResponse(), fromWhere);
                 } else {
                     List<MomentsCommentGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsCommentGetListResponse responce = new MomentsCommentGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MomentsCommentGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
}